// pages/helpDetail/helpDetail.js
const app = getApp();
var WxParse = require('../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: '/banner2.gif',
    banner1: '/banner3.gif',
    banner2: '/banner10.gif',
    detail: '<p>为完善龙江困难家庭救助工作，让精神病人得到及时、适当的疗治，根据《龙江慈善会章程》有关规定，结合我镇的实际，特制定本办法。</p>',
    total_times:0,
    total_money:'0.00',
    list:'',
    pyq:0,
    progress_url:"",
    ifshow:1
  },
  submitData: function (e) {
    var data = e.detail.value;
    if (data.name == "") {
      wx.showToast({
        title: '帮扶联系人不能为空',
        icon: 'none',
        duration: 1500
      })
    } else if (data.phone == "") {
      wx.showToast({
        title: '联系人电话不能为空',
        icon: 'none',
        duration: 1500
      })
    } else if (!(/^1[345678]\d{9}$/.test(data.phone))) {
      wx.showToast({
        title: '联系人电话格式不正确',
        icon: 'none',
        duration: 2000
      });
    } else {
      var userInfo = wx.getStorageSync("userInfo");
      var openid = wx.getStorageSync("openid");
      wx.request({
        url: app.globalData.baseUrl + '/api/index/helpOne',
        data: {
          wx_name: 'zero',
          openid: openid,
          one_id: this.data.id,
          helper_title: '空',
          helper_intro: '空',
          helper_name: data.name,
          helper_mobi: data.phone,
        },
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          console.log(res.data)

        }
      })

      wx.showToast({
        title: '提交成功！',
        icon: 'success',
        duration: 1500
      });

      this.setData({
        status: 1,
        popStatus: false
      })

    }
  },
  bindBeginTimeChange: function (e) {
    this.setData({
      beginTime: e.detail.value
    })
  },
  bindEndTimeChange: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  help: function (e) {
    this.setData({
      popStatus: true
    })
  },
  close: function (e) {
    this.setData({
      popStatus: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var self = this;
    console.log(option);
    self.setData({
      baseUrl: app.globalData.baseUrl,
      bnStatus:option.bnStatus,
      status:option.status,
    })

    if (option.scene>=0 ) {
      var bnStatus = 1;
      self.setData({
        status:option.type,
        id:option.scene,
        bnStatus:bnStatus
      })
    }else{
      self.setData({
        status:option.status,
        id: option.id,
        bnStatus:option.bnStatus
      })
      console.log(option.id);
    }

    var ispyq = wx.getStorageSync("ispyq");
    if (ispyq==1 ) {
      self.setData({
        pyq:1
      })
    }


  if (self.data.status==1) {
    wx.request({
      url: app.globalData.baseUrl + '/api/index/getCishanList?id='+self.data.id,
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        self.setData({
          total_money: res.data.data[0].total_money,
          total_times: res.data.data[0].total_times,
          bTitle:res.data.data[0].title,
          btxtes:res.data.data[0].sub_title,
          time_status:res.data.data[0].status,
          progress_url:res.data.data[0].progress_url
        })
      if (res.data.data[0].donate_list.length!=0) {
        self.setData({
          list: res.data.data[0].donate_list,
        })
      }

        WxParse.wxParse('content', 'html',res.data.data[0].introduction, self, 5);
      }
    })
  }else if(self.data.status==2){
    wx.request({
      url: app.globalData.baseUrl + '/api/index/getDingxiangList?id='+self.data.id,
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        var bnStatus = 1;
        if(res.data.data[0].type == 1)
        {
          bnStatus = 1;
        }else if(res.data.data[0].type == 2)
        {
          bnStatus = 2;
        }else if(res.data.data[0].type == 6)
        {
          bnStatus = 3;
        }else if(res.data.data[0].type == 3)
        {
          bnStatus = 4;
        }else{
          bnStatus = 5;
        }
        var ifshow = 1;
        if(res.data.length == 0)
        {
          ifshow = 2;
        }
        self.setData({
          bnStatus:bnStatus,
          total_money: res.data.data[0].total_money,
          total_times: res.data.data[0].total_times,
          bTitle:res.data.data[0].title,
          btxtes:res.data.data[0].sub_title,
          time_status:res.data.data[0].status,
          progress_url:res.data.data[0].progress_url,
          ifshow:ifshow
        })
      if (res.data.data[0].donate_list.length!=0) {
        self.setData({
          list: res.data.data[0].donate_list,
        })
      }

        WxParse.wxParse('content', 'html',res.data.data[0].introduction, self, 5);
      }
    })
  }else if(self.data.status==4){
    wx.request({
      url: app.globalData.baseUrl + '/api/index/getCishanList?id='+self.data.id,
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        console.log(res.data.data)
        self.setData({
          total_money: res.data.data[0].total_money,
          total_times: res.data.data[0].total_times,
          bTitle:res.data.data[0].title,
          btxtes:res.data.data[0].sub_title,
          time_status:res.data.data[0].status,
          progress_url:res.data.data[0].progress_url
        })
      if (res.data.data[0].donate_list.length!=0) {
        self.setData({
          list: res.data.data[0].donate_list,
        })
      }

        WxParse.wxParse('content', 'html',res.data.data[0].introduction, self, 5);
      }
    })
  }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var openid = wx.getStorageSync("openid");
    console.log(openid)
    if (openid!='') {
      this.setData({
        needAuth: 0
      })
    }else{
      this.setData({
        needAuth: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    console.log(1111);
    var status=that.data.status;
    var id=that.data.id;
    var bnStatus=that.data.bnStatus;

    return {
      title: that.data.bTitle,
      path:'/pages/donate/donate?status='+status+'&id='+id+'&bnStatus='+bnStatus,
      imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  },
  onShareTimeline: function () {
    var that = this;
    var status=that.data.status;
    var id=that.data.id;
    var bnStatus=that.data.bnStatus;
    return {
      title: that.data.bTitle,
      path:'/pages/donate/donate?status='+status+'&id='+id+'&bnStatus='+bnStatus,
      imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    var that= this;
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        console.log(res)
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })

        wx.setStorageSync("userInfo", res.userInfo);
        var rawData = res.rawData;
        var signature = res.signature;
        var encryptData = res.encryptData;
       // var encryptedData = encodeURIComponent(res.encryptedData);
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        wx.setStorageSync("iv", iv);
        wx.setStorageSync("encryptedData", encryptedData);
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            console.log(res.code);
            wx.request({
              url: app.globalData.baseUrl  + '/api/index/jscode2session',
              data: {
                code: res.code,
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" // 默认值
              },
              method: 'POST',
              success(res) {
                var res = res.data;
                console.log(res);
                if (res.status == 1) {
                  that.setData({
                    needAuth:0
                  })
                  wx.setStorage({
                    key: 'token',
                    data: res.data.session_key
                  });
                  wx.setStorage({
                    key: 'openid',
                    data: res.data.openid
                  });
                  wx.setStorageSync("session_key", res.data.session_key);//存到缓存里面openid
                  wx.setStorageSync("openid", res.data.openid);//存到缓存里面openid

                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }

              }
            })
          }
        })
      },
      fail: function() {
          wx.showModal({
              title: "获取信息失败",
              content: "请允许授权以便为您提供给服务",
              success: function(e) {
                  e.confirm && this.getUserProfile();
              }
          });
      }
    })
  },
  bindauth() {
    var that = this;
    var data = that.data.needAuth;
    if (data==1) {
      wx.showModal({
        title: "请您先登录",
        content: "请允许授权以便为您提供给服务",
        success: function(e) {
          if(e.confirm){
            that.getUserProfile();
          }
        }
    });
    }else{
      wx.navigateTo({
        url: '/pages/donateDetail/donateDetail?status='+that.data.status+'&bnStatus='+that.data.bnStatus+'&id='+that.data.id
     })
    }
  }
})